package com.wdh.networklibrary.utils;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadUtil {

    private static ExecutorService service;

    public static ExecutorService getService(){
        if(service == null){
            service = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MICROSECONDS, new LinkedBlockingQueue<>());
        }
        return service;
    }

    public void excute(Runnable runnable){
        service.execute(runnable);
    }

    public static void onDestroy(){
        if(service != null) {
            service.shutdown();
            service = null;
        }
    }








    private static Handler sHandler = new Handler(Looper.getMainLooper());

    // 当前是单一线程池,如需多个线程池改变接口的实现类即可
    private static Executor sExecutor = Executors.newSingleThreadExecutor();

    /**
     * 描述：运行在子线程
     */
    public static void runOnSubThread(Runnable runnable) {
        sExecutor.execute(runnable);
    }

    /**
     * 描述：运行在主线程
     */
    public static void runOnMainThread(Runnable runnable) {
        sHandler.post(runnable);
    }


}
